package org.dubnation.car.controller;

import org.dubnation.car.service.ICarTypeService;
import org.dubnation.car.domain.CarType;
import org.dubnation.car.query.CarTypeQuery;
import org.dubnation.base.util.AjaxResult;
import org.dubnation.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carType")
public class CarTypeController {
    @Autowired
    public ICarTypeService carTypeService;


    /**
     * 保存和修改公用的
     * @param carType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult insertOrUpdate(@RequestBody CarType carType){
        if( carType.getId()!=null)
            carTypeService.update(carType);
        else
            carTypeService.insert(carType);
        return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        carTypeService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id")Long id) {
        CarType carType = carTypeService.queryById(id);
        return AjaxResult.me().setData(carType);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult queryAll(){
        List< CarType> list = carTypeService.queryAll();
        return AjaxResult.me().setData(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
     */
    @PostMapping
    public AjaxResult queryPage(@RequestBody CarTypeQuery query){
        PageInfo<CarType> pageInfo = carTypeService.queryPage(query);
        return AjaxResult.me().setData(pageInfo);
    }

    /**
    * 批量删除数据
    * @param ids
    * @return
*/
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        carTypeService.batchDelete(ids);
        return AjaxResult.me();
    }

    /**
     * 获取车辆类型的树形结构数据
     * @return
     */
    @GetMapping("/treeData")
    public AjaxResult getTreeData() {
        List<CarType> carTypes =  carTypeService.getTreeData();
        return AjaxResult.me().setData(carTypes);
    }


    @GetMapping("/typeTree")
    public AjaxResult getTypeTree() {
        List<CarType> carTypes = carTypeService.queryTypes();
        System.out.println(carTypes);
        return AjaxResult.me().setData(carTypes);
    }

    /**
     * 删除车辆类型 注意：delete请求不能接收对象
     * @param typeId
     * @return
     */
    @DeleteMapping("/deleteType")
    public AjaxResult deleteType(@RequestParam(name = "typeId", required = false) Long typeId) {
        carTypeService.deleteType(typeId);
        return AjaxResult.me();
    }
}
